package com.nny.alarm;

import com.nny.alarm.R;
import com.nny.alarm.alert.AlertTestActivity;
import com.nny.alarm.eidt.EditFragment;
import com.nny.alarm.more.MoreFragment;
import com.uxwine.alarm.Alarm;
import com.uxwine.util.UxToast;
import com.uxwine.view.FoldView;
import com.uxwine.view.SlideView;

import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends FragmentActivity implements SlideView.OnSlideListener
{

	FrameLayout mMainView;
	FoldView mCtnMore;
	SlideView mCtnMain;
	Button mbtnShowMain;

	int[] mnStates;
	int mnCurrent;
	MoreFragment mFrmtMore;
	EditFragment mFrmtMain;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mCtnMain = (SlideView) findViewById(R.id.main_ctn_main);
		mCtnMore = (FoldView) findViewById(R.id.main_ctn_more);

		FragmentManager fm = getSupportFragmentManager();
		mFrmtMore = (MoreFragment) fm.findFragmentById(R.id.main_frg_more);
		mFrmtMain = (EditFragment) fm.findFragmentById(R.id.main_frg_main);

		mCtnMain.setBaseView(mCtnMore);
		mCtnMain.setOnSlideListener(this);
		
		GuideActivity.guide(this, GuideActivity.GUIDE_EDIT);
		//AlertTestActivity.start(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		switch (keyCode)
		{
		case KeyEvent.KEYCODE_VOLUME_DOWN:

		case KeyEvent.KEYCODE_VOLUME_UP:
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed()
	{
		// if(mFrmtMore.onBackPressed())
		// return;
		// if(mFrmtMain.onBackPressed())
		// return;
		if (mCtnMain.isShow())
		{
			mFrmtMain.stop();
			super.onBackPressed();
		} 
		else
		{
			showEidtor(null);
		}
	}

	public void onClickEvent(View v)
	{
		if (v.getId() == R.id.main_btn_more)
		{
			showMore();
			//Uri u = Uri.parse("raw://");
			//Uri u = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
			//UxToast.sshow(this, u.toString());
			
//			RingtoneManager rm = new RingtoneManager(this);
//			rm.setType(RingtoneManager.TYPE_ALARM);
//			Uri u = rm.getRingtoneUri(1);
//			
//			u = Uri.parse("nny://alarm");
//			u = Uri.parse("nny://raw");
//			UxToast.sshow(this, u.toString());
			
		}
	}

	public void showEidtor(Alarm a)
	{
		mFrmtMain.edit(a);
		mCtnMain.toggle();
	}

	public void showMore()
	{
		mCtnMain.toggle();
		if(mFrmtMore.loadAlarms())
			GuideActivity.guide(this, GuideActivity.GUIDE_LIST);
		//Intent i = new Intent(this, GuideActivity.class);
		//startActivity(i);
	}

	@Override
	public void onSliding(SlideView v, int state)
	{
		if (state == SlideView.VIEW_OUT)
		{
			mFrmtMain.hide();
		}
	}
}
